<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.phone" placeholder="电话号码"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="filters.province" placeholder="省份"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="filters.city" placeholder="地市"></el-input>
				</el-form-item>
				<el-form-item label="提单状态" prop="state">
					<el-select v-model="filters.state" placeholder="提单状态" >
						<el-option value="2" label="全部">全部</el-option>
						<el-option value="0" label="允许提单">允许提单</el-option>
						<el-option value="1" label="禁止提单">禁止提单</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getMobilePage">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="fixMobile">更新号段</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="addMobile">添加号段</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="mobiles" highlight-current-row v-loading="listLoading" border max-height="620" style="width: 100%;">
			<el-table-column type="index" width="80" label="编号">
			</el-table-column>
			<el-table-column prop="serviceProvider" label="运营商" >
			</el-table-column>
			<el-table-column prop="province" label="省份名称" >
			</el-table-column>
			<el-table-column prop="city" label="地市名称"  >
			</el-table-column>
			<el-table-column prop="phone" label="电话号码(前7位)"  >
			</el-table-column>
			<el-table-column prop="state" label="是否禁止提单" :formatter="stateFormatter"   >
			</el-table-column>
			<el-table-column label="操作" min-width="250">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button size="small" @click="handleBan(scope.$index, scope.row)"> {{scope.row.state == '0' ? '禁止提单' : '允许提单'}} </el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination
				@current-change="handleCurrentChange"
				@size-change="handleSizeChange"
				layout="total,sizes,prev, pager, next"
				:total="total">
		</el-pagination>

		<!--编辑界面-->
		<el-dialog :title="title" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="100px" :rules="editFormRules" ref="editForm">


				<el-form-item label="运营商" prop="province">
					<el-input v-model="editForm.serviceProvider" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="省份名称" prop="province">
					<el-input v-model="editForm.province" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="地市名称" prop="city">
					<el-input v-model="editForm.city" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="电话号码" prop="phone">
					<el-input v-model="editForm.phone" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
	import util from '../../common/js/util'
	import { getMobilePage,updateMobileStatus,updateMobile,fixMobile} from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					city: '',
					province:'',
					phone:'',
					state:''
				},
                mobiles: [],
                total: 0,
                start:0,
                pageSize:10,
				listLoading: false,

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					city: [
						{required: true, message: '请输入号段地市', trigger: 'blur'}
					]
				},
				//编辑界面数据
				editForm: {
					city: '',
					province: '',
					phone: '',
					id:'',
					serviceProvider:''
				},
				title:"编辑号段",
			}
		},
		methods: {
			addMobile(){
				this.title ="添加号段";
				this.editFormVisible = true;
				this.editForm = {
							city: '',
							province: '',
							phone: '',
							id:'',
							serviceProvider:''
				};

			},
			stateFormatter:function(row, column){
				return  row.state === 0 ? "允许提单" : row.state === 1 ? "禁止提单" : "其他";
			},
			getMobilePage() {
                let para = {
                    start: this.start,
                    pageSize:this.pageSize,
					city: this.filters.city,
					province:this.filters.province,
					phone:this.filters.phone,
                    state:this.filters.state == 2 ? "" : this.filters.state
                };
                this.listLoading = true;
				getMobilePage(para).then((datas) => {
                    this.listLoading = false;
                    let { message, code, data } = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.mobiles = data.result;
                        this.total = data.totalCount;
                    }
                });
			},fixMobile(){
				fixMobile().then((datas) => {
					this.listLoading = false;
					if(datas == "ok"){
                        this.$message({
                            message: datas,
                            type: 'info'
                        });
                        this.getMobilePage();
					}else {
                        this.$message({
                            message: "失败",
                            type: 'error'
                        });
					}
				});
			}
			,handleCurrentChange:function(val){
				this.start = val - 1;
				this.getMobilePage();
			},
			handleSizeChange:function(val){
				this.pageSize = val;
				this.getMobilePage();
			},handleEdit:function(index,row){
				this.title ="编辑号段";
				this.editFormVisible = true;
				this.editForm.city = row.city;
				this.editForm.province = row.province;
				this.editForm.phone = row.phone;
				this.editForm.id = row.id;
				this.editForm.serviceProvider = row.serviceProvider;
			},editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
							updateMobile(para).then((res) => {
								this.editLoading = false;
								let {message, code, data} = res;
								if (code !== "0") {
									this.$message({
										message: message,
										type: 'error'
									});
								} else {
									this.$message({
										message: '提交成功',
										type: 'success'
									});
								}
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.getMobilePage();
							});
						});
					}
				});
			},handleBan:function (index,row) {
				this.$confirm('确认更新该号段状态吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					let para = {
						state: row.state == "0" ? "1" : "0",
						id: row.id
					};
					updateMobileStatus(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						let {message, code, data} = res;
						if (code !== "0") {
							this.$message({
								message: message,
								type: 'error'
							});
						}
						this.$message({
							message: '操作成功',
							type: 'success'
						});
						this.getMobilePage();
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getMobilePage();
		}
	}

</script>

<style scoped>

</style>
